package com.schedule.controller;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.reflect.TypeToken;
import com.schedule.entity.Count;
import com.schedule.entity.Friends;
import com.schedule.entity.User;
import com.schedule.service.FriendsService;

@Controller
public class FriendsController {
	@Autowired
	FriendsService friendsService;
	
	@ResponseBody
	@RequestMapping(value="/AddFriends.action",produces = "text/json;charset=UTF-8")
	public Object AddFriends(@RequestParam String param) {
		/*
		JsonParser jp=new JsonParser();
		JsonObject jo_daa=jp.parse(param).getAsJsonObject();
		String user=jo_daa.get("user").getAsString();
		String send=jo_daa.get("send").getAsString();
		String request=jo_daa.get("request").getAsString();
		Friends friends=new Friends();
		friends.setUser(user);
		friends.setSend(send);
		friends.setRequest(request);
		*/
		
		Gson gson=new Gson();
		Friends friends1=gson.fromJson(param, new TypeToken<Friends>(){}.getType()); 	
		friendsService.AddFriends(friends1);
		
		Map<String, Object> result=new HashMap<String, Object>();
		result.put("code", 1);
		return new Gson().toJson(result);
	}
	
	@ResponseBody
	@RequestMapping(value="/FindFriends.action",produces="text/json;charset=UTF-8")
	public Object FindFriends(@RequestParam String user) {
		Map<String, Object> result=new HashMap<String, Object>();
		List<User> list=friendsService.FindFriends(user);
		result.put("code", 1);
		result.put("data", list);		
		return new Gson().toJson(result);
	}
	
	@ResponseBody
	@RequestMapping(value="/deleteFriends.action",produces = "text/json;charset=UTF-8")
	public Object deleteFriends(@RequestParam String param) {
		
		JsonParser jp=new JsonParser();
		JsonObject jo_daa=jp.parse(param).getAsJsonObject();
		String user=jo_daa.get("user").getAsString();
		String phone=jo_daa.get("phone").getAsString();
		
		friendsService.deleteFriends(user,phone);
		
		
		Map<String, Object> result=new HashMap<String, Object>();
		result.put("code", 1);
		return new Gson().toJson(result);
	}
	
	@ResponseBody
	@RequestMapping(value="/deleteFriends2.action",produces = "text/json;charset=UTF-8")
	public Object deleteFriends2(@RequestParam String param) {
		
		JsonParser jp=new JsonParser();
		JsonObject jo_daa=jp.parse(param).getAsJsonObject();
		String send=jo_daa.get("send").getAsString();
		String phone=jo_daa.get("phone").getAsString();
		
		friendsService.deleteFriends2(send,phone);
		
		//System.out.println("12");
		Map<String, Object> result=new HashMap<String, Object>();
		result.put("code", 1);
		return new Gson().toJson(result);
	}

}
